package com.xjh.basestudy.designmode.createMode.createMode3_FactoryMethod.p2_simplyfactory;

import com.xjh.basestudy.designmode.createMode.createMode3_FactoryMethod.p1_original.Airplane;
import com.xjh.basestudy.designmode.createMode.createMode3_FactoryMethod.p1_original.Enemy;
import com.xjh.basestudy.designmode.createMode.createMode3_FactoryMethod.p1_original.Tank;

import java.util.Random;

/**
 * @author XuJianHua
 * @date 2022-09-22 15:32:44
 * @description 简单工厂 通过传入不同参数决定创建飞机/坦克
 */
public class SimpleFactory {
    private int screenWidth;
    private Random random;//随机数

    public SimpleFactory(int screenWidth) {
        this.screenWidth = screenWidth;
        this.random = new Random();
    }

    public Enemy create(String type){
        int x = random.nextInt(screenWidth);//生成敌人横坐标随机数
        Enemy enemy = null;
        switch (type) {
            case "Airplane":
                enemy = new Airplane(x, 0);//实例化飞机
                break;
            case "Tank":
                enemy = new Tank(x, 0);//实例化坦克
                break;
        }
        return enemy;
    }

}